import java.io.InterruptedIOException;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.SwingWorker;

public class FillWorker implements Runnable
{

	Faculty fac;
	String college;

	public FillWorker(Faculty fac, String college)
	{
		this.fac = fac;
		this.college = college;
	}
	/* (non-Javadoc)
	 * @see javax.swing.SwingWorker#doInBackground()
	 */
	@Override
	public void run()
	{

		System.out.println("****Filling Faculty****");
		StringBuffer stafflist = new StringBuffer("");
		HTMLRetriever htmlretriever = new HTMLRetriever();
		stafflist = htmlretriever.getHTML(fac.getAddress());
		Strategy strategy = testStrategies(stafflist);
		Context context = new Context(strategy);
		fac.setStaff(context.execute(stafflist).getStaff());
		removeFalsePositives(fac);
		
		Thread t = new Thread(new QueryWorker(fac, college)); 
		t.start();	
	}
	
	public Strategy testStrategies(StringBuffer stafflist)
	{
		int maxResults;
		Strategy strat;
		Faculty faculty;
		Context test = new Context(new DCUStrategy());
		System.out.println("dcu start");
		faculty = test.execute(stafflist);
		System.out.println("end");
		maxResults = faculty.getTotalStaffMembers();
		maxResults = maxResults + sanityTestFaculty(faculty);
		System.out.println(maxResults);
		strat = new DCUStrategy();		
		test = new Context(new TCDStrategy());
		System.out.println("tcd start");
		faculty = test.execute(stafflist);
		System.out.println("tcd end");
		System.out.println(faculty.getTotalStaffMembers());
		if(faculty.getTotalStaffMembers()+ sanityTestFaculty(faculty) > maxResults)
		{
			maxResults = faculty.getTotalStaffMembers();
			strat = new TCDStrategy();
		}
		
		test = new Context(new UCDStrategy());
		System.out.println("ucd start");
		faculty = test.execute(stafflist);
		System.out.println("ucd end");
		System.out.println(faculty.getTotalStaffMembers());
		if(faculty.getTotalStaffMembers()+ sanityTestFaculty(faculty) > maxResults)
		{
			maxResults = faculty.getTotalStaffMembers();
			strat = new UCDStrategy();
		}
		test = new Context(new ULStrategy());
		System.out.println("ul start");
		faculty = test.execute(stafflist);
		System.out.println("ul end");
		System.out.println(faculty.getTotalStaffMembers());
		if(faculty.getTotalStaffMembers()+ sanityTestFaculty(faculty) > maxResults)
		{
			maxResults = faculty.getTotalStaffMembers();
			strat = new ULStrategy();
		}

		return strat;
		
	}
    
	public int sanityTestFaculty(Faculty f)
	{
		Pattern commonNames = Pattern.compile("(m(i(chael|ke))|urphy|ark)|(j(o(hn|e))|im|ane)|david|(r(yan|obert|ichard))|ann(e|ette)|(l(iam|ee|orraine))|gerard|(w(ang|ei|ong))|(c(hris|ollins|ahill))|gary|finn|hogan|scott|zhang|tony", Pattern.CASE_INSENSITIVE);
		int found = 0;
		for(int i = 0; i< f.getTotalStaffMembers(); i++)
		{
			Matcher m = commonNames.matcher(f.returnNames());
			while(m.find())
			{
				found += 10;
			}
		}
		
		return found;
	}
    
	public void removeFalsePositives(Faculty fac)
	{
		String optimized = "(\\d)|"+fac.getName()+"|"+fac.getType()+"|(\\s)use|a(bout|ssociate)|b(oard|sc)|co(mput|ntact|urse)|d((e(gree|partment|sk))|(i(ploma|scrimination)))|graduate|h((o(me|norary))|ighlight)|in(dex|formation|tranet)|l(earn|og(\\s)?in)|module|ne(twork|ws)|p(ro(blem|fession|ject)|(age|eople|hd))|re(s(earch|ource)|(port))|s((i(gn\\sin|te))|(t(aff|ud(ent|y)))|(u(mmer|pport))|chool|earch)|technical|visit";
		Pattern falsePositives = Pattern.compile(optimized, Pattern.CASE_INSENSITIVE);
		Iterator<Person> it = fac.getStaff().iterator();
		int index = 0;
		while(it.hasNext()) 
		{
			Person p = it.next();
			Matcher incorrect = falsePositives.matcher(p.getName());
			if(incorrect.find())
			{
				it.remove();
			}
			index++;
		}
		
		fac.getStaff().trimToSize();
	}
}
